package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.AnnounceDao;
import com.resourcestore.model.Announce;
import com.resourcestore.utils.tag.Pagination;

public class AnnounceAction extends BaseAction {

	private Announce announce;
	private List<Announce> listAnnounce;
	private AnnounceDao announceDao = new AnnounceDao();
	private Pagination pagination = new Pagination();

	/**
	 * 查询返回List
	 * 
	 * @throws ParseException
	 * 
	 */
	public String listAnnounce() {
		announce = new Announce();
		pagination.setTotal(announceDao.queryAnnounceNum(announce));
		listAnnounce = announceDao.selectAnnouncePage(announce, pagination);

		for (int i = 0; i < listAnnounce.size(); i++) {
			try {
				listAnnounce.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listAnnounce.get(i)
								.getGmtCreated()));
				listAnnounce.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listAnnounce.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return "default";
	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListAnounce() {

		// 对内容进行urldecoder解码
		try {
			announce.setAnnounceName(new String(announce.getAnnounceName()
					.getBytes("iso-8859-1"), "UTF-8"));
			announce.setAnnounceContent(new String(announce
					.getAnnounceContent().getBytes("iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(announceDao.queryAnnounceNum(announce));
		listAnnounce = announceDao.selectAnnouncePage(announce, pagination);

		// 将bigint数字处理为时间格式

		for (int i = 0; i < listAnnounce.size(); i++) {
			try {
				listAnnounce.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listAnnounce.get(i)
								.getGmtCreated()));
				listAnnounce.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listAnnounce.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/Announce/listAnnounce.jsp");
	}

	/**
	 * 预插入一条数据
	 */
	public String preInsertAnnounce() {

		return newpath("/Announce/insertAnnounce.jsp");
	}

	/**
	 * 插入一条数据
	 */
	public String insertAnnounce() {

		try {
			announce.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			announce.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		announceDao.insertAnnounce(announce);
		return redirect(this.getUrl());
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateAnnounce() {

		announce = announceDao.selectAnnounceById(announce);

		return newpath("/Announce/updateAnnounce.jsp");
	}

	/**
	 * 公告详情
	 * 
	 */
	public String detailAnnounce() {

		announce = announceDao.selectAnnounceById(announce);

		return newpath("/Announce/detailAnnounce.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateAnnounce() {

		try {
			// announce.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			announce.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		announceDao.updateAnnounce(announce);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteAnnounce() {
		announceDao.deleteAnnounce(announce);
		return redirect(this.getUrl());
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreAnnounce() {
		announceDao.deleteMoreAnnounce(announce);
		return redirect(this.getUrl());
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public Announce getAnnounce() {
		return announce;
	}

	public void setAnnounce(Announce announce) {
		this.announce = announce;
	}

	public List<Announce> getListAnnounce() {
		return listAnnounce;
	}

	public void setListAnnounce(List<Announce> listAnnounce) {
		this.listAnnounce = listAnnounce;
	}

}
